package com.ruoyi.asset.controller;

import java.util.List;

import com.ruoyi.asset.domain.AstDeviceRecord;
import com.ruoyi.asset.service.IAstDeviceRecordService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.AstDevice;
import com.ruoyi.asset.service.IAstDeviceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author xunzhe
 * @date 2024-06-07
 */
@Controller
@RequestMapping("/asset/astDevice")
public class AstDeviceController extends BaseController
{
    private String prefix = "asset/astDevice";

    @Autowired
    private IAstDeviceService astDeviceService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IAstDeviceRecordService deviceRecordService;

    @RequiresPermissions("asset:astDevice:view")
    @GetMapping()
    public String astDevice()
    {
        return prefix + "/astDevice";
    }

    /**
     * 查询设备管理列表
     */
    @RequiresPermissions("asset:astDevice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AstDevice astDevice)
    {
        startPage();
        List<AstDevice> list = astDeviceService.selectAstDeviceList(astDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @RequiresPermissions("asset:astDevice:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AstDevice astDevice)
    {
        List<AstDevice> list = astDeviceService.selectAstDeviceList(astDevice);
        ExcelUtil<AstDevice> util = new ExcelUtil<AstDevice>(AstDevice.class);
        return util.exportExcel(list, "设备管理数据");
    }

    /**
     * 新增设备管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备管理
     */
    @RequiresPermissions("asset:astDevice:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AstDevice astDevice) {
        astDevice.setCreateBy(getLoginName());
        astDevice.setDeptId(getSysUser().getDeptId());
        try {
            List<String> strings = astDeviceService.insertAstDevice(astDevice);
            return AjaxResult.success("操作成功",strings);
        }catch (Exception e){
            return AjaxResult.error(e.toString());
        }
    }

    /**
     * 修改设备管理
     */
    @RequiresPermissions("asset:astDevice:edit")
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap)
    {
        AstDevice astDevice = astDeviceService.selectAstDeviceByDeviceId(deviceId);
        mmap.put("astDevice", astDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备管理
     */
    @RequiresPermissions("asset:astDevice:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AstDevice astDevice)
    {
        return toAjax(astDeviceService.updateAstDevice(astDevice));
    }

    /**
     * 删除设备管理
     */
    @RequiresPermissions("asset:astDevice:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(astDeviceService.deleteAstDeviceByDeviceIds(ids));
    }
}
